var database = require('../../data/data.js');
var app = getApp();
Page({
  data: {
    tabbar: {},
    flag: true,
    pageNum: 1,
    noData: false,
  },
  onLoad: function () {
    var count = 0;
    var all = 2;
    wx.showLoading({
      title: '资源加载中',
      mask: true
    })
    var that = this;
    app.editTabBar();
    wx.login({
      success(res) {
        var code = res.code;
        wx.request({
          url: database.url + "/api/V2/user/access_token",
          data: {
            clientId: "QqKTO5BTPN6REI26QAgq",
            clientSecret: "F2MjbLO88FMiwr3pTOXylnjURAOzvqLJXSFMeJUg"
          },
          success(res) {
            database.accessToken = res.data.data.accessToken;
            that.setData({
              pageNum: 1
            })
            var pageNum = that.data.pageNum
            if (database.unionid == "") {
              wx.request({
                url: database.url + '/api/V2/integral/integral_openid',
                method: "POST",
                header: {
                  'content-type': 'application/x-www-form-urlencoded'
                },
                data: {
                  code: code,
                  accessToken: database.accessToken,
                  weChatId: "DB80F6CF1D0D4D1AB8F7B5C0A04CA704",
                  regionName: "other"
                },
                success(res) {
                  var sessoionKey = res.data.data.session_key;
                  wx.getUserInfo({
                    data: {
                      withCredentials: true,
                      lang: "zh_CN"
                    },
                    success(res) {
                      wx.request({
                        url: database.url + '/api/V2/user/we_chat_user',
                        method: "POST",
                        header: {
                          'content-type': 'application/x-www-form-urlencoded'
                        },
                        data: {
                          accessToken: database.accessToken,
                          weChatId: "DB80F6CF1D0D4D1AB8F7B5C0A04CA704",
                          encryptedData: res.encryptedData,
                          iv: res.iv,
                          sessionKey: sessoionKey,
                          weChatName: "足球投票"
                        },
                        success(res) {
                          database.unionid = res.data.data.unionId;
                          wx.request({
                            url: database.url + '/api/V2/integral/integral_list',
                            method: "POST",
                            header: {
                              'content-type': 'application/x-www-form-urlencoded'
                            },
                            data: {
                              accessToken: database.accessToken,
                              regionName: "other",
                              pageNo: pageNum,
                              pageSize: 10,
                              unionid: database.unionid
                            },
                            success(res) {
                              ++pageNum;
                              var integralList = res.data.data.integralList;
                              that.setData({
                                integralList: integralList,
                                pageNum: pageNum
                              })
                              if (integralList.length < 10) {
                                that.setData({
                                  noData: true
                                })
                              }
                              ++count;
                              if (count == all) {
                                wx.hideLoading();
                              }
                            }
                          })
                          wx.request({
                            url: database.url + '/api/V2/user/verify_wechat_small',
                            method: "POST",
                            header: {
                              'content-type': 'application/x-www-form-urlencoded'
                            },
                            data: {
                              accessToken: database.accessToken,
                              unionid: database.unionid
                            },
                            success(res) {
                              console.log(res)
                              if (res.data.data.verify == "yes") {
                                database.userId = res.data.data.id
                                database.login = true;
                                wx.request({
                                  url: database.url + '/api/V2/softball/softball_list',
                                  method: "POST",
                                  header: {
                                    'content-type': 'application/x-www-form-urlencoded'
                                  },
                                  data: {
                                    accessToken: database.accessToken,
                                    userId: database.userId
                                  },
                                  success(res) {
                                    ++count;
                                    if (count == all) {
                                      wx.hideLoading();
                                    }
                                    database.have_team = true;
                                  }
                                })
                              } else {
                                ++count;
                                if (count == all) {
                                  wx.hideLoading();
                                }
                                database.login = false;
                              }
                            }
                          })
                        }
                      })
                    }
                  })
                }
              })
            } else {
              wx.request({
                url: database.url + '/api/V2/user/verify_wechat_small',
                method: "POST",
                header: {
                  'content-type': 'application/x-www-form-urlencoded'
                },
                data: {
                  accessToken: database.accessToken,
                  unionid: database.unionid
                },
                success(res) {
                  console.log(res)
                  if (res.data.data.verify == "yes") {
                    database.userId = res.data.data.id
                    database.login = true;
                    wx.request({
                      url: database.url + '/api/V2/softball/softball_list',
                      method: "POST",
                      header: {
                        'content-type': 'application/x-www-form-urlencoded'
                      },
                      data: {
                        accessToken: database.accessToken,
                        userId: database.userId
                      },
                      success(res) {
                        ++count;
                        if (count == all) {
                          wx.hideLoading();
                        }
                        database.have_team = true;
                      }
                    })
                  } else {
                    ++count;
                    if (count == all) {
                      wx.hideLoading();
                    }
                    database.login = false;
                  }
                }
              })
              wx.request({
                url: database.url + '/api/V2/integral/integral_list',
                method: "POST",
                header: {
                  'content-type': 'application/x-www-form-urlencoded'
                },
                data: {
                  accessToken: database.accessToken,
                  regionName: "other",
                  pageNo: pageNum,
                  pageSize: 10,
                  unionid: database.unionid
                },
                success(res) {
                  ++pageNum;
                  var integralList = res.data.data.integralList;
                  that.setData({
                    integralList: integralList,
                    pageNum: pageNum
                  })
                  if (integralList.length < 10) {
                    that.setData({
                      noData: true
                    })
                  }
                  ++count;
                  if (count == all) {
                    wx.hideLoading();
                  }
                }
              })
            }
          }
        })
      }
    })
  },
  onShow() {
    var that = this;
    if (database.vote.length > 0) {
      var integralList = this.data.integralList
      for (var i = 0; i < this.data.integralList.length; i++) {
        for (var j = 0; j < database.vote.length; j++) {
          if (database.vote[j].id == integralList[i].id) {
            integralList[i].integralTotal = parseInt(integralList[i].integralTotal) + 1;
          }
        }
      }
      database.vote = [];
      this.setData({
        integralList: integralList
      })
    }
    if (database.search) {
      wx.showLoading({
        title: '资源加载中',
        mask: true
      })
      this.setData({
        pageNum: 1
      })
      var pageNum = that.data.pageNum
      wx.request({
        url: database.url + '/api/V2/integral/integral_list',
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          accessToken: database.accessToken,
          regionName: "other",
          pageNo: pageNum,
          pageSize: 10,
          unionid: database.unionid
        },
        success(res) {
          ++pageNum;
          var integralList = res.data.data.integralList;
          that.setData({
            integralList: integralList,
            pageNum: pageNum
          })
          if (integralList.length < 10) {
            that.setData({
              noData: true
            })
          }
          wx.hideLoading();
        }
      })
    }
  },
  search: function () {
    database.search = true;
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },
  benyuejiangpin: function () {
    wx.navigateTo({
      url: '/pages/jiangpin_detail/jiangpin_detail',
    })
  },
  jifenguize: function () {
    wx.navigateTo({
      url: '/pages/guize_detail/guize_detail',
    })
  },
  lishipaiming: function () {
    wx.navigateTo({
      url: '/pages/history/history',
    })
  },
  toDetail: function (e) {
    wx.navigateTo({
      url: '/pages/team_detail/team_detail?id=' + e.currentTarget.dataset.id + '&type=vote',
    })
  },
  add: function () {
    if (database.login) {
      if (!database.have_team) {
        this.setData({
          flag: false,
          prompt_img: "/img/gantan.png",
          prompt_text: "请先创建至少一支球队",
          prompt_bt1: "创建球队",
          prompt_bt2: "暂不创建",
          prompt_tap: "toCreate"
        })
      } else {
        wx.showToast({
          title: '您已参加该活动!',
        })
      }
    } else {
      this.setData({
        flag: false,
        prompt_img: "/img/gantan.png",
        prompt_text: "请先绑定手机",
        prompt_bt1: "绑定手机",
        prompt_bt2: "暂不绑定",
        prompt_tap: "toBinding"
      })
    }
  },
  hide: function () {
    this.setData({
      flag: true
    })
  },
  toBinding: function () {

    this.setData({
      flag: true
    })
    wx.navigateTo({
      url: '/pages/binding/binding',
    })
  },
  toCreate: function () {
    this.setData({
      flag: true
    })
    wx.navigateTo({
      url: '/pages/my/myteam/createteam/createteam',
    })
  },
  vote: function (e) {
    var that = this;
    wx.showLoading({
      title: '正在保存',
    })
    var id = e.currentTarget.dataset.id;
    wx.request({
      url: database.url + '/api/V2/integral/integral_vote',
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        accessToken: database.accessToken,
        regionName: "other",
        softballId: id,
        weChatId: "DB80F6CF1D0D4D1AB8F7B5C0A04CA704",
        unionid: database.unionid,
        integralGenre: "TouPiao"
      },
      success(res) {
        if (res.data.status_code == 200) {
          var teamList = that.data.integralList;
          for (var i = 0; i < teamList.length; i++) {
            if (teamList[i].id == id) {
              teamList[i].integralTotal = parseInt(teamList[i].integralTotal) + 1
            }
          }
          that.setData({
            integralList: teamList
          })
        } else {

        }
        wx.hideLoading();
        wx.showToast({
          icon: "none",
          title: res.data.status_reason,
        })
      }
    })
  },
  onReachBottom: function () {
    var that = this;
    var integralList1 = [];
    var pageNum = that.data.pageNum;
    if (!this.data.noData) {
      wx.showLoading({
        title: '资源加载中',
        mask: true
      })
      wx.request({
        url: database.url + '/api/V2/integral/integral_list',
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          accessToken: database.accessToken,
          regionName: "other",
          pageNo: pageNum,
          pageSize: 10,
          unionid: database.unionid
        },
        success(res) {
          ++pageNum;
          var integralList2 = res.data.data.integralList;
          for (var i = 0; i < that.data.integralList.length + integralList2.length; i++) {
            if (i < that.data.integralList.length) {
              integralList1[i] = that.data.integralList[i];
            } else {
              integralList1[i] = integralList2[i - that.data.integralList.length];
            }
          }
          that.setData({
            integralList: integralList1,
            pageNum: pageNum
          })
          if (integralList2.length < 10) {
            that.setData({
              noData: true
            })
          }
          wx.hideLoading();
        }
      })
    }
  },
  onShareAppMessage: function () {
    var that = this;
    return {
      title: '参与全国足球队投票排名 获取足球装备大奖',
      imageUrl: '/img/homeShare.png',
      path: '/pages/home/home',
      success: function (res) {
        wx.showToast({
          title: '转发成功',
          icon: 'success',
          duration: 1500
        })
      }
    }
  }
})